<template>
	<view>
		<nav-header title="设置手机号" :showRight="false"></nav-header>
		<view class="nickname">
			<view class="form">
				<view class="title">
					<text class="text-sm-Medium">手机号</text>
					<text class="text-sm-Regular" style="color: #A4A7AE;" v-if="fontNum!=0">{{fontNum}}/30</text>
					<text class="text-sm-Regular" style="color: #A4A7AE;" v-else>{{countNickName}}/30</text>
				</view>
				<uni-forms :modelValue="userInfo" :rules="rules">
					<uni-forms-item name="userName">
						<uni-easyinput type="text" @input="sumFontNum" v-model="userInfo.userName" placeholder="请输入内容"
							:inputBorder="false" clearable maxlength="11"/>
					</uni-forms-item>
				</uni-forms>
				<view class="tip">
					<text class="text-xs-Regular">*请填写手机号码</text>
				</view>
			</view>
			<view class="commit">
				<view class="button-style">
					<button class="btncolor" @click="submitForm()">
						<text class="text-md-Medium" style="color: #fff;">保存</text>
					</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		regexPhone
	} from '@/utils'
	export default {
		data() {
			return {
				userInfo: {},
				rules: {
					userName: {
						rules: [{
							required: true,
							errorMessage: '请输入登录名',
						}]
					},
				},
				fontNum: 0,
				countNickName: 0
				
			}
		},
		computed: {
			userNameDisable() {
				return regexPhone.test(this.userInfo.userName)
			}
		},
		methods: {
			submitForm() {
				if(this.userInfo.userName == ""){
					uni.showToast({
						title: "手机号不能为空",
						icon: 'none'
					});
					return;
				}
				if (this.userNameDisable) {
					this.$http({
						url: `/user/update/${this.userInfo.userName}`,
						method: "PUT",
					}).then(() => {
						this.userStore.setUserInfo(this.userInfo);
						uni.setStorageSync("userName", this.userInfo.userName);
						uni.showToast({
							title: "保存成功",
							icon: 'none'
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: "请正确填写手机号"
					})
				}
			},
			sumFontNum(e) {
				this.fontNum = e.length
			}
		},
		onLoad() {
			// 深拷贝一份数据
			let mine = this.userStore.userInfo;
			this.userInfo = JSON.parse(JSON.stringify(mine));
			this.$set(this.userInfo, 'isTmp', false)
			this.countNickName = this.userInfo.userName.length
		}
	}
</script>

<style lang="scss" scoped>
	.nickname {
		width: 100%;

		.form {
			display: flex;
			flex-direction: column;
			padding: 24rpx 32rpx;
			gap: 24rpx;

			:deep(.uni-forms) {
				height: 88rpx;
			}

			:deep(.uni-easyinput__content-input) {
				height: 40rpx;
				padding: 24rpx 32rpx;
				background-color: $gray-100;
			}

			.title {
				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.tip {
				display: flex;
				align-items: center;
				justify-content: space-between;
				// font-size: 25rpx;
				color: $gray-400;
			}
		}



		.commit {
			width: 100%;
			position: fixed;
			bottom: 42rpx;
			left: 0;
			display: flex;
			justify-content: center;
			align-items: center;

			.button-style {
				padding: 24rpx 32rpx;
				width: 100%;
			}

			.btncolor[disabled] {

				background-color: $brand-400;
			}

			.btncolor {
				width: 100%;
				background-color: $brand-500;
				color: $white;
				border-radius: 34rpx;
				padding: 8rpx 24rpx;
				line-height: 60rpx;
				text-align: center;
			}
		}
	}
</style>